package com.jerry.utils.str;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author Created by 谭健 on 2019/1/7. 星期一. 13:07. © All Rights Reserved.
 */
public class JsonUtils {
	
	private static final Logger log = LoggerFactory.getLogger(JsonUtils.class);

	public static JSONObject readJsonFromClassPath(String path) throws IOException {

		ClassPathResource resource = new ClassPathResource(path);
		if (resource.exists()) {
			return JSON.parseObject(resource.getInputStream(), StandardCharsets.UTF_8, JSONObject.class,
					// 自动关闭流
					Feature.AutoCloseSource,
					// 允许注释
					Feature.AllowComment,
					// 允许单引号
					Feature.AllowSingleQuotes,
					// 使用 Big decimal
					Feature.UseBigDecimal);
		} else {
			throw new IOException();
		}
	}

	public static void main(String[] args) {
		JSONObject jObject = new JSONObject();
		try {
			jObject = JsonUtils.readJsonFromClassPath("test.json");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		log.debug(""+jObject.getJSONArray("result").size());
	}
}